package com.liang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liang.shortlink.project.dao.entity.LinkOsStatsDO;
import com.liang.shortlink.project.dto.req.ShortLinkGroupStatsReqDTO;
import com.liang.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * 操作系统统计访问持久层
 */
public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {

    /**
     * 短链接地区访问监控数据
     */
    @Insert("""
            INSERT INTO t_link_os_stats (full_short_url, date, cnt, os, create_time, update_time, del_flag)
            VALUES (#{param.fullShortUrl}, #{param.date}, #{param.cnt}, #{param.os}, NOW(), NOW(), 0)
            ON DUPLICATE KEY
                UPDATE cnt  = cnt + #{param.cnt};""")
    void shortLinkOsStats(@Param("param") LinkOsStatsDO linkOsStatsDO);

    /**
     * 根据短链接获取指定日期内操作系统监控数据
     */
    @Select("""
            SELECT tlos.os,
                   SUM(tlos.cnt) AS count
            FROM t_link tl
                     INNER JOIN t_link_os_stats tlos
                                ON tl.full_short_url = tlos.full_short_url
            WHERE tlos.full_short_url = #{param.fullShortUrl}
              AND tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = #{param.enableStatus}
              AND tlos.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tlos.full_short_url, tl.gid, tlos.os;
            """)
    List<HashMap<String, Object>> listOsStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内操作系统监控数据
     */
    @Select("""
            SELECT tlos.os,
                   SUM(tlos.cnt) AS count
            FROM t_link tl
                     INNER JOIN t_link_os_stats tlos
                                ON tl.full_short_url = tlos.full_short_url
            WHERE tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = '0'
              AND tlos.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tl.gid, tlos.os;
            """)
    List<HashMap<String, Object>> listOsStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);
}
